package cc.chinagps.gateway.unit.eg.upload.bean;

import cc.chinagps.gateway.unit.eg.pkg.EGPackage;
import cc.chinagps.gateway.unit.eg.util.ConvUtil;
import cc.chinagps.gateway.unit.eg.util.EGTimeFormatUtil;
import cc.chinagps.gateway.util.HexUtil;
import lombok.Data;
import org.seg.lib.util.Util;

import java.util.ArrayList;
import java.util.List;

@Data
public class EGRealTimeInfo {
    private long collectTime;
    private String collectTimeStr;
    private int serialNo;
    private EGObdInfo egObdInfo;
    private EGEngineDataStreamInfo dataStreamInfo;
    private EGSmokeDetectInfo egSmokeDetectInfo;
    private EGSuZhouInfo egSuZhouInfo;
    private List<Integer> statusList = new ArrayList<Integer>();
    private boolean isHistory;
    private int softwareVersion;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("EGRealTimeInfo [collectTime=");
        builder.append(collectTime);
        builder.append(", collectTimeStr=");
        builder.append(collectTimeStr);
        builder.append(", serialNo=");
        builder.append(serialNo);
        builder.append(", egObdInfo=");
        builder.append(egObdInfo);
        builder.append(", dataStreamInfo=");
        builder.append(dataStreamInfo);
        builder.append(", egSmokeDetectInfo=");
        builder.append(egSmokeDetectInfo);
        builder.append(", egSuZhouInfo=");
        builder.append(egSuZhouInfo);
        builder.append(", statusList=");
        builder.append(statusList);
        builder.append(", isHistory=");
        builder.append(isHistory);
        builder.append(", softwareVersion=");
        builder.append(softwareVersion);
        builder.append("]");
        return builder.toString();
    }

    public static EGRealTimeInfo parse(byte[] data, int offset) throws Exception {
        EGRealTimeInfo realTimeInfo = new EGRealTimeInfo();
        int position = offset;

        String collectTime = "20" + cc.chinagps.gateway.util.Util.getDateTime(data, position, 6);
        realTimeInfo.setCollectTimeStr(collectTime);
        realTimeInfo.setCollectTime(EGTimeFormatUtil.parseGMT8(collectTime).getTime());
        position += 6;

        int serialNo = Util.getShort(data, position);
        realTimeInfo.setSerialNo(serialNo);
        position += 2;

        EGEngineDataStreamInfo dataStreamInfo = null;
        while (position < data.length) {
            if (position == data.length - 1) {
                break;
            }
            int infoType = data[position++] & 0xFF;
            switch (infoType) {
                case 0x01:
                    EGObdInfo egObdInfo = new EGObdInfo();
                    egObdInfo = egObdInfo.parse(data, position);
                    realTimeInfo.setEgObdInfo(egObdInfo);
                    position += egObdInfo.getDataLen();
                    break;
                case 0x02:
                    dataStreamInfo = new EGEngineDataStreamInfo();
                    dataStreamInfo = dataStreamInfo.parse(dataStreamInfo, data, position);
                    position += dataStreamInfo.getDataLen();
                    break;
                case 0x80:
                    if (dataStreamInfo == null) {
                        dataStreamInfo = new EGEngineDataStreamInfo();
                    }
                    dataStreamInfo = dataStreamInfo.parseSupply(dataStreamInfo, data, position);
                    position += dataStreamInfo.getSupplyDataLen();
                    break;
                case 0x90:
                    EGSmokeDetectInfo egSmokeDetectInfo = new EGSmokeDetectInfo();
                    egSmokeDetectInfo = egSmokeDetectInfo.parse(data, position);
                    realTimeInfo.setEgSmokeDetectInfo(egSmokeDetectInfo);
                    position += egSmokeDetectInfo.getDataLen();
                    break;
                case 0x91:
                    if (dataStreamInfo == null) {
                        dataStreamInfo = new EGEngineDataStreamInfo();
                    }
                    int accStatus = data[position++];
                    if (accStatus == 1) {
                        realTimeInfo.getStatusList().add(33);
                    } else if (accStatus == 0) {
                        realTimeInfo.getStatusList().add(23);
                    }

                    Double instantFuelD = ConvUtil.getDouble(data, position, (short) 0xFFFF, 512, 3);
                    if (instantFuelD == null) {
                        //dataStreamInfo.setInstantFuel(StartServer.obdNullVal);
                    } else if (instantFuelD != 0) {
                        dataStreamInfo.setInstantFuel((int) (10000 / instantFuelD));
                    }

                    position += 2;
                    position += 4;
                    int course = Util.getShort(data, position);
                    dataStreamInfo.setCourse(course);
                    position += 2;
                    break;
                case 0x92:
                    EGSuZhouInfo egSuZhouInfo = new EGSuZhouInfo();
                    egSuZhouInfo = egSuZhouInfo.parse(data, position);
                    realTimeInfo.setEgSuZhouInfo(egSuZhouInfo);
                    realTimeInfo.getStatusList().addAll(egSuZhouInfo.getStatus());
                    position += egSuZhouInfo.getDataLen();
                    break;
                default:
                    break;
            }
        }
        realTimeInfo.setDataStreamInfo(dataStreamInfo);
        return realTimeInfo;
    }

    public static void main(String[] args) throws Exception {
        String s = "2323024c53374a53475657394a42303031323236220100a11404150d35044ae2010200000000004c53374a53475657394a4230303132323600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000cb7d8300000000ffffffffb8ffff0960ffffffff5d4a000736012101d71d1200050b36800100000050ef38ffffffffffffffffffff15";
        s = "2323024c53374a53475657394a42303031323236220100a11404150d2d044902010200000000004c53374a53475657394a4230303132323600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000cb7d8300000000ffffffffb8ffff0960ffffffff5e4a000736012c01d71d1c00050b36800100000050ef38ffffffffffffffffffffee";
        s = "2323023030303030303030303030303030303030020100b414051a120405008701020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000b883ff18f800000fa00fa0faffffffffffffffff6dff0006b92c0a021dddae00027ace800fff0000001144ffffffff00000000ffff9000070020000100069101000000000000000021";
        s = "2323023030303030303030303030303030303030020100b414051b092706016701020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020600ba96ff224800280fa02454faffffffffffffffff76ff0006b9f267021d581600027ace800fff0000066a3effffffff00000000ffff900022008b00060007910100000000000000008c";
        s = "2323023030303030303030303030303030303030020100b41406040b0804008f01020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000022700b8b2ff2aa00078ffff0fa0b90a1433203560001e77000006b99f3e021e05870000886880011c00002fe045ffffffff00000000ffff9000160023002c00039101000000000000000040";
        s = "2323023230323046525033344B30484A56313933010100411408120A1E01002D026500CAB9B961C8043854D854D8414CB87C607C20341279410006C9559B015905930002326C9000000000000000019101000000000000000063";
        s = "2323023230323046525033344b30484a56313933010100411408120a30010051026500cab9b961c8043854d854d8414cb87c607c20341279410006c9559e015905960002326c9000000000000000019101000000000000000031";
        s = "2323024c52445336504542374c52303031343232030100671408120b3412010200000000004c52445336504542374c523030313432323030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030300047";
        s = "23230230303030303030303030303030303030300c0100a1140a160a030603880100000000000030303030303030303030303030303030300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002ffffffffff0000ffffffffffffffffffffffffffffffffff0007320e4601ca148effffffff80ffffffffffffffffffffffffffffffffff72";
        s = "2323024c4657535258524a384b4144323032343001010067140b0a0e0d00010200000000004c4657535258524a384b414432303234303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030300070";
        s = "23230230303030303030303030303030303030300e0100be140b120c1635296d01020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000ca818518f800000fa0fffffa000039d03766000078050006cfbcfe015a1ef500001af48009ff000000b7ff0000000000000000222003ee000000000000003d000004990afa0000030303001f01000000000023";
        s = "2323023030303030303030303030303030303030010100b41502010b2732023601020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000023b00b0d88537881a2c0fa0931cf0ffffffffffff000076000006528bd60188c0a000298182800c640000007535ffffffff00000000ffff9000000000000000069101000000000000000016";
        s = "2323023030303030303030303030303030303030010100b41502010b2714023501020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000023d00b2d8ff38401a2c0fa08d18faffffffffffff000072000006528b540188d05400298182800c640000007435ffffffff00000000ffff90000000000000000691010000000000000000f9";
        s = "23230230303030303030303030303030303030301b0100ab1503170c15371ff201020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000ca818518f800000fa0ffff98000039d03766000078050006cfbcf2015a1ef500001af48009ff000000b742000000000000000022209101000000000000000064";
        s = "23230230303030303030303030303030303030301b0100ab1503170d3b3236e401020000000000303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000ca8185190800000fa0fffffa000039d03766000078050006cfbcf1015a1ef000001af48009ff000000b7ff00000000000000002220910100000000000001346c";
        s = "232302575738363732353830353931313038333901010041150c070c002c0040020000ffffffffffffffffffffffffffffffffffffffffffff00015a035c06cb4f1f000000009001c200190080177091010000ffffffff0000a3";
        s = "2323025757383637323538303539313130383339010100b4150c070f0f2b000101fe0000000000575738363732353830353931313038333930303030303030303030303030303030303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000000000000000020000ffffffffffffffffffffffffffffffffffffffffffff01015a033f06cb4f15000000008009ffffffffffffffffffffffffffffffff9001c200190080177091010000ffffffff00003a";
        //s = "232302303030303030303030303030303030303001010069150c070f21170002010200000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030004d";
        s = "2323025757383637323538303539313130383339010100b4150c0710022a003201020000000000575738363732353830353931313038333930303030303030303030303030303030303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000000000000000020000ffffffffffffffffffffffffffffffffffffffffffff00015a034106cb4f13000000008009ffffffffffffffffffffffffffffffff9001c200190080177091010000ffffffff00009f";
        s = "2323024c47364544415348314859323030393931050100c31601040f3a230065010200000000004c4736454441534831485932303039393130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000007d7d000000000fa00fa000000022202220000028000006cfbd0e015a1eed0000000080090000000000280000000000000000222092000005220000052200000000052200000000000522000091000000000000000000fe";
        s = "2323024c31323334353637383941414142424243050100c316010510100f9c1c010000000000ff4c31323334353637383941414142424243534e30313233343536373839303132333030534e30313030303030303030303030303030534e3031323334353637383930313233340000000000000000000000000000000000000000020000007d7d82e000000fa00fa0000a80222022200000719d0006cfbcfe015a1ef0000001458009000000070728000000000000000022209200000707000000080000000100000007000000000001459101000000000000000019";
        s = "2323024c5a5a4942585344334b44353734353239150100b4180b0b0f2310007a010200000000004c5a5a4942585344334b4435373435323930303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000020000c2898212e800280fa026a491101840004040ffff730000069a7d42021714ee000ec54a8000000381d5a639ffffffff00000000ffff90000200170001000091010000000000000000f0";
        EGPackage egPackage = EGPackage.parse(HexUtil.hexToBytes(s));
        System.out.println(EGRealTimeInfo.parse(egPackage.getData(), 0));
    }

}
